package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.SpendPoint;
import cn.wolfcode.crm.query.JSONResult;
import cn.wolfcode.crm.service.ISpendPointItemService;
import cn.wolfcode.crm.service.ISpendPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("spendPoint")
public class SpendPointController {

    @Autowired
    private ISpendPointService spendPointService;

    @Autowired
    private ISpendPointItemService spendPointItemService;

    //由于dataGrid组件返回必须为json,所以跳转界面就分开
    @RequestMapping("view")
    public String view(){
        return "spendPoint";
    }

    /*@RequestMapping("spendAnalyze")
    public String spendAnalyze(){
        return "spendAnalyze";
    }*/

    //页面数据
    @RequestMapping("query")
    @ResponseBody
    public List<SpendPoint> query(){
        return spendPointService.selectAll();
    }


    //名称检验
    @RequestMapping("validateByName")
    @ResponseBody
    public Boolean validateByName(Long id, String name){
        return spendPointService.selectByName(id,name);
    }

    //编辑
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(SpendPoint spendPoint){
        JSONResult result = new JSONResult();
        try{
            spendPointService.saveOrUpdate(spendPoint);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
    //删除
    @RequestMapping("delete")
    @ResponseBody
    public JSONResult delete(Long id){
        JSONResult result = new JSONResult();
        try{
            spendPointService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }


}
